package edu.predatorwars.client.visualization;

import com.badlogic.gdx.math.Vector3;


public class MathExt {
	public static Vector3 calcNormal(Vector3 v1, Vector3 v2, Vector3 v3) {
		float v1x, v1y, v1z, v2x, v2y, v2z;
		float nx, ny, nz;
		float vLen;
		
		// Calculate vectors
		  v1x = v1.x - v2.x;
		  v1y = v1.y - v2.y;
		  v1z = v1.z - v2.z;
		 
		  v2x = v2.x - v3.x;
		  v2y = v2.y - v3.y;
		  v2z = v2.z - v3.z;
		 
		  // Get cross product of vectors
		  nx = (v1y * v2z) - (v1z * v2y);
		  ny = (v1z * v2x) - (v1x * v2z);
		  nz = (v1x * v2y) - (v1y * v2x);
		 
		  // Normalise final vector
		  vLen = (float) Math.sqrt( (nx * nx) + (ny * ny) + (nz * nz) );
		
		return new Vector3(nx / vLen, ny / vLen, nz/vLen);
	}
	

	public static FPoint calcPositionFromArrayLoc(int X, int Y, int len1D, int len2D) {
		//return new FPoint(calcXPositionFromArrayLoc(X, len1D), calcYPositionFromArrayLoc(X, Y, len1D, len2D));
		return new FPoint(calcXPositionFromArrayLoc(X, Y, len1D, len2D),calcYPositionFromArrayLoc(X, len1D));
	}
	
	public static float calcXPositionFromArrayLoc(int X, int Y, int len1D, int len2D) {

		float mod;
		int relx = -(int) Math.floor((double)len1D / 2d) + X;
		int rely = -(int) Math.floor((double) len2D / 2d) + Y;
		
		if (relx > 0) mod = -1;
		else mod=1;
		
		return rely*0.41f+(mod*relx%2f)*0.2f;
	}
	
	public static float calcYPositionFromArrayLoc(int X, int len) {
		int relx = -(int) Math.floor((double)len/ 2d) + X;
		return  -relx*0.32f;
	}
	
}